import os
import csv
import time
import random

print time.strftime('%X %x'), '  Start'

os.chdir('/Users/kavita/Desktop/')
print 'Current Folder = ',os.getcwd()

# --- READ Pathways MASTER LIST ---
pathways = list(csv.reader(open("pathways.csv", "r")))	# read unique pathways into list
pathways = [ x[0] for x in pathways ]
print time.strftime('%X %x'), "  Pathways File Read Complete\n"

# --- READ miRNA MASTER LIST (only those in pathways) ---
mirnas = list(csv.reader(open("miRNAs.csv", "r")))	# read unique miRNAs into list
mirnas = [ x[0] for x in mirnas ]
print time.strftime('%X %x'), "  miRNA File Read Complete\n"

# --- READ RELATIONS FILE INTO LIST ---
# "Pathway","Gene ID","Gene Name","Copies in Path","Transcript","miRNA","Score","Start","End"
#  0         1         2           3                4            5       6       7       8
relation = list(csv.reader(open("relation.csv", "r")))  # get relations
relation = relation[1:]                                 # discard header
print time.strftime('%X %x'), "  Relation File Read Complete\n"

# --- BUILD EMPTY DICTIONARY OF miRNAs and Pathways FROM RELATIONS (there may be some we're not interested in, i.e., not in pathways) ---
allRelMirs = set([])
allRelPaths = set([])
for r in relation:
    allRelMirs.add(r[5])
    allRelPaths.add(r[0])
print 'Unique miRNAs in relation file = ',len(allRelMirs)
print 'Unique Pathways in relation file = ',len(allRelPaths)
mir_scores  = dict([ (x,[]) for x in list(allRelMirs) ])        # make dictionary from list of miRNAs (one list for each mir)
mir_targets = dict([ (x,[]) for x in list(allRelMirs) ])
print time.strftime('%X %x'), "  Empty miRNA Dictionaries Complete\n"
path_genes = dict([ (x,set([])) for x in list(allRelPaths) ])   # make dictionary from list of Paths (one set for each path)
path_mirs  = dict([ (x,set([])) for x in list(allRelPaths) ])
print time.strftime('%X %x'), "  Empty Pathway Dictionaries Complete\n"

# --- POPULATE DICTIONARIES ---
for r in relation:                                              # go through relations once and populate all dictionaries
    mir_scores[ r[5] ].append( [r[0],r[6]] )
    mir_targets[ r[5] ].append( [r[2],r[6]] )
    path_genes[ r[0] ].add( r[2] )
    path_mirs[ r[0] ].add( r[5] )
print time.strftime('%X %x'), "  mir_scores Dictionary Complete\n"
print time.strftime('%X %x'), "  mir_targets Dictionary Complete\n"
for key in path_genes.keys():                                   # convert sets to lists (just to be nice)
    path_genes[key] = list(path_genes[key])
    path_mirs[key] = list(path_mirs[key])
print time.strftime('%X %x'), "  path_genes Dictionary Complete\n"
print time.strftime('%X %x'), "  path_mirs Dictionary Complete\n"

print pathways
print mir_targets


